# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\acusensor\__init__.py
import os.path, io, zipfile, logging, gevent.lock
from settings_provider import settings
from server.http_responses.http_errors import Http500

_logger = logging.getLogger("helpers.acusensor")
sensor_dotnet = None
sensor_php = None
semaphore = None


def string_replace_generator(input_data, secret, step=2048):
    part = b""
    end = len(input_data)
    for i in range(0, end, step):
        part += input_data[i : i + step]
        part = part.replace(b"################################", secret)
        if part.endswith(b"#"):
            continue
        yield part
        part = b""


def get_acusensor(sensor_type, secret):
    global semaphore
    global sensor_dotnet
    global sensor_php
    if sensor_type == "net":
        if sensor_dotnet is None:
            sensor_storage = settings.get(
                "storage.sensors", os.path.join("data", "acusensor")
            )
            path = os.path.join(sensor_storage, "AcuSensorInstaller.msi")
            if not os.path.isfile(path):
                _logger.error("Sensor %s not found", path)
                raise Http500("Sensor %s not found" % (sensor_type,))
            data = open(path, "rb").read()
            if b"082119f75623eb7abd7bf357698ff66c" not in data:
                _logger.error("Sensor %s incorrect start password", path)
                raise Http500("Sensor %s not found" % (sensor_type,))
            sensor_dotnet = data.replace(
                b"082119f75623eb7abd7bf357698ff66c", b"################################"
            )
        return (
            "AcuSensorInstaller.msi",
            string_replace_generator(sensor_dotnet, secret),
        )
    if sensor_type == "php":
        if sensor_php is None:
            sensor_storage = settings.get(
                "storage.sensors", os.path.join("data", "acusensor")
            )
            path = os.path.join(sensor_storage, "acu_phpaspect.php")
            if not os.path.isfile(path):
                _logger.error("Sensor %s not found", path)
                raise Http500("Sensor %s not found" % (sensor_type,))
            data = open(path, "rb").read()
            if b"082119f75623eb7abd7bf357698ff66c" not in data:
                _logger.error("Sensor %s incorrect start password", path)
                raise Http500("Sensor %s not found" % (sensor_type,))
            sensor_php = data.replace(
                b"082119f75623eb7abd7bf357698ff66c", b"################################"
            )
        return ("acu_phpaspect.php", string_replace_generator(sensor_php, secret))
    if sensor_type == "java":
        sensor_storage = settings.get(
            "storage.sensors", os.path.join("data", "acusensor")
        )
        path = os.path.join(sensor_storage, "AcuSensor.jar")
        if not os.path.isfile(path):
            _logger.error("Sensor %s not found", path)
            raise Http500("Sensor %s not found" % (sensor_type,))
        if semaphore is None:
            semaphore = gevent.lock.Semaphore(5)
        if not semaphore.acquire(1):
            raise Http500("Service temporally unavailable")
        try:
            zin = zipfile.ZipFile(path, "r")
            output = io.BytesIO()
            with zipfile.ZipFile(output, "w") as (z_out):
                z_out.comment = zin.comment
                for item in zin.infolist():
                    if item.filename != "acu.properties":
                        z_out.writestr(item, zin.read(item.filename))

                z_out.writestr("acu.properties", b"password=" + secret)
            output.seek(0)
            data = output.read()
            output.close()
        finally:
            semaphore.release()

        return ("AcuSensor.jar", data)
    raise Http500("invalid sensor_type %s" % sensor_type)
